package cn.ulc.socket.admin.service.member.impl;

import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.member.WxUserDTO;
import cn.ulc.socket.admin.model.dto.member.WxUserListDTO;
import cn.ulc.socket.admin.service.member.WxUserServ;
import cn.ulc.store.dao.user.WeixinUserDao;
import cn.ulc.store.entity.staff.Staff;
import cn.ulc.store.entity.user.WeixinUser;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxUserServImpl extends BaseService implements WxUserServ {
    @Autowired
    WeixinUserDao wxUserDao;

    @Override
    public WxUserListDTO findList(String key, PaginationParam param) {
        Staff loginUser = getCurrentUser();
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",key);
        condition.put("companyId",loginUser.getCompanyId());
        PageQuery pageQuery = initPageQuery(param);
        pageQuery.setParas(condition);
        wxUserDao.findListPage(pageQuery);
        return new WxUserListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public WxUserDTO findDetail(Long id) {
        WeixinUser entity = wxUserDao.findOne(id);
        if (entity == null) throw new PlatformException("获取会员信息失败!");
        return new WxUserDTO(entity);
    }

    @Override
    public Boolean deleted(Long id) {
        WeixinUser member = wxUserDao.findOne(id);
        if(member==null) throw new PlatformException("获取会员信息失败!");
        setBaseInfo(member);
        member.setDeleted(DeletedStatus.DELETED);
        wxUserDao.save(member);
        return true;
    }
}
